/**
 * Created by huyoufu on 2021/7/6 17:42.
 * contact me -->https://www.jk1123.com
 */


#ifndef ALGORITHM_LIST_H
#define ALGORITHM_LIST_H

#endif //ALGORITHM_LIST_H
//获取某个结构体 类型的偏移位置
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
//根据结构体的某个属性的内存位置获取 该对象的内存位置
#define container_of(ptr, type, member) ( {\
const typeof( ((type*)0)->member ) *__mptr=(ptr);\
(type*)( (char*)__mptr - offsetof(type,member) );})

//定义一个双向连接 而且是环形双向连接
struct head_link_list {
    struct head_link_list *prev;
    struct head_link_list *next;
};
typedef struct head_link_list hllist;
#define LINK_LIST_INIT(name) {&(name),&(name)}

#define LIST_HEAD(name) \
struct head_link_list name = LINK_LIST_INIT(name)

static inline void INIT_LIST_HEAD(struct head_link_list *head) {
    head->next = head;
    head->prev = head;
}


static inline void _head_link_list_add(hllist *prev, hllist *next, hllist *add) {
    next->prev = add;
    add->next = next;
    prev->next = add;
    add->prev = prev;
}

//添加元素 到链表中 head是头节点 这里我们采用头插法
static inline void head_link_list_add(head_link_list *head, head_link_list *add) {
    _head_link_list_add(head, head->next, add);
}

//添加元素 到链表中 head是头节点 这里我们采用尾插法
static inline void head_link_list_tail(head_link_list *head, head_link_list *add) {
    _head_link_list_add(head->prev, head, add);
}

static inline void __list_del(hllist *prev, hllist *next) {
    next->prev = prev;
    prev->next = next;
}

//删除一个元素
static inline void list_del(hllist *node) {
    __list_del(node->prev, node->next);
}
//遍历列表
#define list_for_each(node, head)\
for(node=head->next;node!=head;node=node->next)
//安全的遍历链:
#define list_for_each_safe(node,s,head)\
for(node=head->next,s=node->next;node!=head;node=s,s=s->next)












